//引入express模块
const express = require('express');
//引入ejs模块
const ejs = require('ejs');
//引入sha1加密
const sha1 = require('sha1');
// 引入reply模块
const reply = require('../reply');
//引入wechat模块
const Wechat = require('../wechat/wechat');
//引入config模块
const { url } = require('../connfig');
const {
    homeDatas,
    homeBottom,
    fithessDatas,
} = require('../api')
    //获取Router
const Router = express.Router;
//创建路由对象
const router = new Router();
//创建实例对象
const wechatApi = new Wechat();
//页面路由
router.get('/search', async(req, res) => {
    //获取随机字符串
    let num = Math.random() + '';
    let noncestr = num.split('.')[1];
    //获取时间戳
    const timestamp = Date.now();
    //获取票据
    const {
        ticket
    } = await wechatApi.fetchTicket()

    //组合四个参数
    const arr = [
        `jsapi_ticket=${ticket}`,
        `noncestr=${noncestr}`,
        `timestamp=${timestamp}`,
        `url=${url}/search`
    ]

    //进行字典排序，以&符连接
    const str = arr.sort().join('&');
    console.log(str);
    //进行sha1加密
    const signature = sha1(str);
    console.log(signature);

    res.render('search', {
        signature,
        noncestr,
        timestamp,
    })
});
//首页页面内容
router.get('/home', async(req, res) => {
    res.render('home', {
        homeDatas,
        homeBottom,
    })
});
router.get("/", async(req, res) => {
    res.render('long', {
        homeDatas,
        homeBottom,
    })
})
router.get("/mine", async(req, res) => {

    res.render('mine', {
        homeBottom,
    })
})
router.get("/fitness", async(req, res) => {
    res.render('fitness', {
        fithessDatas,
        homeBottom,
    })
})
router.get("/long", async(req, res) => {
    res.render('long', {
        fithessDatas,
        homeBottom,
    })
})
router.use(reply());

module.exports = router